package chpt12;

/**
 * 批处理测试：
 * 不支持查询select操作
 * 支持update,insert,delete操作
 * 
 */
import java.sql.*;

public class BatchTest {
	public static void main(String[] args) {
		String url = "jdbc:mysql://127.0.0.1:3306/demoDB?serverTimezone=Asia/Chongqing";
		String userName = "root";
		String pwd = "root123";
		String sql1 = "insert into students "
				+ "values(null, 'Js114','陈某',2.55,null)";
		String sql2 = "insert into students "
				+ "values(null, 'Js122','李某', 3.6,null)";
		String sql3 = "update students set gpa=2 where name like '李%'";
		try(Connection con = DriverManager.getConnection(
				url, userName, pwd);
				Statement stmt = con.createStatement();) {
			// String sql0 = "delete from student where id='1111'";
			//"select * from student"; //不支持select
			// state.addBatch(sql0);
			stmt.addBatch(sql1);
			stmt.addBatch(sql2);
			stmt.addBatch(sql3);

			int[] recNum = new int[3]; // 保存每个SQL语句执行后更新的记录数
			recNum = stmt.executeBatch();
			System.out.println("sql1处理的记录数：" + recNum[0]);
			System.out.println("sql2处理的记录数：" + recNum[1]);
			System.out.println("sql3处理的记录数：" + recNum[2]);
			System.out.println("结束！");
		} catch (SQLException e) {
			e.printStackTrace();
		} 
	}
}
